﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZuoYe6.Models;

namespace ZuoYe6.Controllers
{
    public class DefaultController : Controller
    {
        Model1 model1 = new Model1();
        // GET: Default
        public ActionResult Index()
        {
            var list = model1.Task.ToList();
            return View(list);
        }
        public ActionResult XiangQing(int id)
        { 
            Task task = model1.Task.FirstOrDefault(p => p.TaskID == id);
            return View(task);
        }
        [HttpPost]
        public ActionResult Delete(int id)
        {
            Task task = model1.Task.FirstOrDefault(p => p.TaskID == id);
            if (task != null)
            {
                model1.Task.Remove(task);
                var result = model1.SaveChanges() > 0;
                return Json(result);
            }
            return Json(false);
        }
        public ActionResult Edit(int id)
        {
            var v = model1.Task.FirstOrDefault(p => p.TaskID == id);
            return View(v);
        }
        [HttpPost]
        public ActionResult Edit(Task task)
        {
            
            if (ModelState.IsValid)
            {
                var v = model1.Task.FirstOrDefault(p => p.TaskID == task.TaskID);
                v.TaskPriority = task.TaskPriority;
                v.Content = task.Content;
                v.AddTime = task.AddTime;
                v.TaskState = task.TaskState;
                v.FinishedTime = task.FinishedTime;
                v.uid = task.uid;
                var result = model1.SaveChanges();
                if (result > 0)
                {
                    return RedirectToAction("Index");
                }
        }
            return View(task);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Task task)
        {
            if (ModelState.IsValid)
            {
                model1.Task.Add(task);
                var result = model1.SaveChanges();
                if (result > 0)
                {
                    return RedirectToAction("Index");
                }
            }
            return View(task);
        }
    }
}